
/*
 * Copyright (c) Honor Device Co., Ltd. 2024-2024. All rights reserved.
 */

package com.wangfugui.mybatisplus.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * SystemUtil
 *
 * @since 2024-05-07
 */
public class SystemUtil {
    private static Logger log = LoggerFactory.getLogger(SystemUtil.class);

    /**
     * cleanString
     *
     * @param str 请求参数
     * @return String
     */
    public static String cleanString(String str) {
        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); ++i) {
            sb.append(cleanChar(str.charAt(i)));
        }
        return sb.toString();
    }

    private static char cleanChar(char ch) {

        // 0 - 9
        for (int i = 48; i < 58; ++i) {
            if (ch == i) {
                return (char) i;
            }
        }

        // 'A' - 'Z'
        for (int i = 65; i < 91; ++i) {
            if (ch == i) {
                return (char) i;
            }
        }

        // 'a' - 'z'
        for (int i = 97; i < 123; ++i) {
            if (ch == i) {
                return (char) i;
            }
        }

        // other valid characters
        switch (ch) {
            case '/':
                return '/';
            case '\\':
                return '\\';
            case '.':
                return '.';
            case '-':
                return '-';
            case '_':
                return '_';
            case ',':
                return ',';
            case ' ':
                return ' ';
            case '!':
                return '!';
            case '@':
                return '@';
            case '#':
                return '#';
            case '$':
                return '$';
            case '%':
                return '%';
            case '^':
                return '^';
            case '&':
                return '&';
            case '*':
                return '*';
            case '(':
                return '(';
            case ')':
                return ')';
            case '+':
                return '+';
            case '=':
                return '=';
            case ':':
                return ':';
            case ';':
                return ';';
            case '?':
                return '?';
            case '"':
                return '"';
            case '<':
                return '<';
            case '>':
                return '>';
            case '`':
                return '`';
        }
        if (isChineseChar(ch)) {
            return ch;
        }
        log.error("[ALARM] Unrecognized character, Please check the path of input file name");
        return '%';
    }

    // 根据Unicode编码判断中文汉字和符号
    private static boolean isChineseChar(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
            || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION;
    }

    /**
     * 验证文件路径
     *
     * @param input input
     * @return String
     */
    public static String verifyNonNullString(String input) {
        char[] originalChars = input.toCharArray();
        char[] chars = new char[originalChars.length];
        for (int i = 0; i < originalChars.length; i++) {
            chars[i] = purifyChar(originalChars[i]);
        }
        return new String(chars);
    }

    private static char purifyChar(char inputc) {
        return inputc;
    }

    /**
     * 检查是否为unixos
     *
     * @return boolean
     */
    public static boolean isNotWindows() {
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 比较版本号的大小
     *
     * @param version1 版本1
     * @param version2 版本2
     * @return 前者大则返回一个正数, 后者大返回一个负数, 相等则返回0
     */
    public static int compareVersion(String version1, String version2) {
        String[] versionArray1 = version1.split("\\.");
        String[] versionArray2 = version2.split("\\.");
        int idx = 0;
        int diff = 0;
        int minLength = Math.min(versionArray1.length, versionArray2.length);
        while (idx < minLength && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0
            && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {
            ++idx;
        }

        // 如果已经分出大小，则直接返回，如果未分出大小，则再比较位数，有子版本的为大；
        diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
        return diff;
    }

}
